<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="$router.push({name: 'menu-created'})">添加菜单</el-button>
      </div>
      <el-table
      :data="tableData"
      style="width: 100%">
        <el-table-column
          prop="id"
          label="编号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="菜单名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="level"
          label="菜单级数">
        </el-table-column>
        <el-table-column
          prop="icon"
          label="前端图标">
        </el-table-column>
        <el-table-column
          prop="orderNum"
          label="排序">
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getAllMenu, delMenu } from '@/services/menu'

export default {
  name: 'Menu',
  data () {
    return {
      tableData: []
    }
  },
  created () {
    this.loadAllMenu()
  },
  methods: {
    async loadAllMenu () {
      const { data } = await getAllMenu()
      this.tableData = data.data
    },
    handleEdit (index, row) {
      this.$router.push({
        name: 'menu-edit',
        params: {
          id: row.id
        }
      })
    },
    handleDelete (index, row) {
      this.$confirm('确认删除？', '删除提示')
        .then(async () => {
          const { data } = await delMenu(row.id)
          // 确认是否删除成功
          if (data.code === '000000') {
            this.$message.success('删除成功')
            // 更新列表
            this.loadAllMenu()
          }
        })
        .catch(() => {
          this.$message('取消删除')
        })
    }
  }
}
</script>

<style scoped>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }
</style>
